import {reqMember, deleUser, addUser,editUser} from '@/service/member';
import {message} from 'antd';

const Model = {
  namespace: 'member',
  state: {
    member: [],
    total:0
  },
  effects: {
    * reqMember({payload}, {put,call}) {
      const res = yield call(reqMember, payload)
      if (res.code === 200) {
        yield put({type: 'saveMember',payload: res.raw});
        yield put({type: 'saveTotal',payload: res.total});
      }
    },

    *deleUser({payload},{call,put}){
      const res = yield call(deleUser,payload.id)
      if(res.code===200){
        message.success(`删除用户 ${payload.username} 成功`)
        yield put({
          type:'reqMember',
          payload:{
            current: payload.page,
            limit: payload.limit
          }
        })
      }else{
        message.error(`删除用户 ${payload.username} 失败  ${res.msg}`)
      }
    },
    
    
    * addUser({payload}, {put,call}) {
      const res = yield call(addUser, payload)
      if (res.code === 200) {
        message.success(`添加用户 ${payload.username} 成功`)
      }else{
        message.error(`添加用户 ${payload.username} 失败  ${res.msg}`)
      }
    },

    * editUser({payload}, {put,call}) {
    const res = yield call(editUser, payload)
    if (res.code === 200) {
      message.success(`修改用户 ${payload.username} 成功`)
    }else{
      message.error(`修改用户 ${payload.username} 失败  ${res.msg}`)
    }
  },
  },
  reducers: {
    saveMember(state, {payload}) {
        // console.log('me');
      return {...state,member: payload}
    },
    saveTotal(state, {payload}) {
        // console.log(payload);
      return {...state,total: payload}
    }
  }
}
export default Model
